<?php

namespace Acme\FilesManagerBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;

use Acme\FilesManagerBundle\FM\Classes\File;
use Acme\FilesManagerBundle\FM\Classes\Folder;

// these import the "@Route" and "@Template" annotations
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

class FilesController extends Controller
{
private $homeDir="C:\\xampp\\htdocs\\filesmanager\\" ;
    /**
     * @Route("/", name="_file_manage")
     * @Template()
     */
    public function indexAction()
    {
			$session = $this->getRequest()->getSession();
			$session->set('currentDir',$this->homeDir);
			$mianfolders=array();
			$folders=array();
			$files=array();
			
			$folder=new Folder($this->homeDir);
			
			foreach ($folder->getFolders() as $subFolder){
				//var_dump($subFolder);
				$mianfolders[]=$subFolder->getName();
			}
			$i=0;
			foreach ($folder->getFolders() as $subFolder){
				$folders[]=array('index'=>$i,'name'=>$subFolder->getName(),'modifyDate'=>$subFolder->getModifyDate(),'size'=>$subFolder->getSize(),'filescount'=>$subFolder->filesCount(),'folderscount'=>$subFolder->foldersCount());
				$i++;
				
			}
			
			foreach ($folder->getFiles() as $file){
				$files[]=array('index'=>$i,'name'=>$file->getName(),'modifyDate'=>$file->getModifyDate(),'size'=>$file->getSize(),'downloads'=>$file->getDownloadCount());
				$i++;
			}
		return array(
			'folder'=>$folder->getName(),
			'countFiles'=>$folder->filesCount()+$folder->foldersCount(),
			'mianfolders'=>$mianfolders,
			'folders'=>$folders,
			'files'=>$files,
			'copyDate'=>date('Y'),
			'copyName'=>$this->homeDir
		);
    }
	  /**
     * @Route("/browse/{folder}", name="_file_manage_browse")
     * @Template()
     */
	 
    public function browseAction($folder)
    {
			$folder1=$folder;
			$mianfolders=array();
			$folders=array();
			$files=array();
			
			$session = $this->getRequest()->getSession();
			$session->set('currentDir',$session->get('currentDir').$folder1."/");
			$folder=new Folder($session->get('currentDir'));
			
			
			foreach ($folder->getFolders() as $subFolder){
				//var_dump($subFolder);
				$mianfolders[]=$subFolder->getName();
			}
			$i=0;
			foreach ($folder->getFolders() as $subFolder){
				$folders[]=array('index'=>$i,'name'=>$subFolder->getName(),'modifyDate'=>$subFolder->getModifyDate(),'size'=>$subFolder->getSize(),'filescount'=>2,'folderscount'=>$subFolder->foldersCount());
				$i++;
				
			}
			foreach ($folder->getFiles() as $file){
				$files[]=array('index'=>$i,'name'=>$file->getName(),'modifyDate'=>$file->getModifyDate(),'size'=>$file->getSize(),'downloads'=>$file->getDownloadCount());
				$i++;
			}
		return array(
			'folder'=>$folder->getName(),
			'countFiles'=>$folder->filesCount()+$folder->foldersCount(),
			'mianfolders'=>$mianfolders,
			'folders'=>$folders,
			'files'=>$files,
			'copyDate'=>date('Y'),
			'copyName'=>$session->get('currentDir')
		);
    }
	/**
     * @Route("/back", name="_file_manage_back")
     * @Template()
     */
	public function backAction(){
			
			$response ="";	
			$session = $this->getRequest()->getSession();
			if($session->get('currentDir')!=$this->homeDir){
			
				$session->set('currentDir',dirname($session->get('currentDir'))."/");
				$folder=basename($session->get('currentDir'));
				$session->set('currentDir',dirname($session->get('currentDir'))."/");
				$response = $this->forward('AcmeDemoBundle:Files:browse', array('folder'  => $folder));	
			}else
			$response= $this->forward('AcmeDemoBundle:Files:index');
			
		return 	$response;				
	}
	/**
     * @Route("/download/{file}", name="_file_manage_download")
     * @Template()
     */
    public function downloadAction($file)
    {
		$session = $this->getRequest()->getSession();
		$file=$session->get('currentDir').$file;
	
	
		header("Pragma: public");
		header('Content-disposition: attachment; filename='.basename($file));
		header("Content-type: application/force-download");
		header('Content-Transfer-Encoding: binary');
			ob_clean();
			flush();
			readfile($file); 
		
			
		return new Response();
    }
	/**
     * @Route("/deletefile/{file}", name="_file_manage_deletefile")
     * @Template()
     */
    public function deletefileAction($file)
    {
		$session = $this->getRequest()->getSession();
		$file=$session->get('currentDir').$file;
			$f=new File($file);
			$f->delete();
			$message="File is Deleted";
		return new Response($message);
    }
	/**
     * @Route("/deletefolder/{folder}", name="_file_manage_deletefolder")
     * @Template()
     */
    public function deletefolderAction($folder)
    {
		$session = $this->getRequest()->getSession();
		$folder=$session->get('currentDir').$folder;
			$f=new Folder($folder);
			$f->delete();
			$message="Folder is Deleted";
		return new Response($message);
    }
	/**
     * @Route("/saerch/{name}", name="_file_manage_saerch")
     * @Template()
     */
    public function saerchAction($file)
    {
		return new Response("Downloading ...");
    }
	/**
     * @Route("/renameFile/{file}", name="_file_manage_renameFile")
     * @Template()
     */
    public function renameFileAction($file)
    {
		$name=$this->getRequest()->request->get('name');
		$session = $this->getRequest()->getSession();
		$f=new File($session->get('currentDir').$file);
		$f->rename($name);
		return new Response("Ok");
    }
	/**
     * @Route("/renameFolder/{folder}", name="_file_manage_renameFolder")
     * @Template()
     */
    public function renameFolderAction($folder)
    {
		$name=$this->getRequest()->request->get('name');
		$session = $this->getRequest()->getSession();
		$f=new Folder($session->get('currentDir').$folder);
		$f->rename($name);
		return new Response("Ok");

    }
	/**
     * @Route("/createFolder/{folder}", name="_file_manage_createFolder")
     * @Template()
     */
    public function createFolderAction($folder)
    {
		
		$session = $this->getRequest()->getSession();
		mkdir($session->get('currentDir').$folder);
		
		return new Response("Ok");

    }
	/**
     * @Route("/refresh", name="_file_manage_refresh")
     * @Template()
     */
    public function refreshAction()
    {
		$response ="";	
			$session = $this->getRequest()->getSession();
			if($session->get('currentDir')!=$this->homeDir){
				$folder=basename($session->get('currentDir'));
				$session->set('currentDir',dirname($session->get('currentDir'))."/");
				$response = $this->forward('AcmeDemoBundle:Files:browse', array('folder'  => $folder));	
			}else
			$response= $this->forward('AcmeDemoBundle:Files:index');
			
		return 	$response;			

    }
		/**
     * @Route("/upload", name="_file_manage_upload")
     * @Template()
     */
    public function uploadAction()
    {
		$response ="";	
			$session = $this->getRequest()->getSession();
			$target_path = $session->get('currentDir');
			$target_path = $target_path . basename( $_FILES['file']['name']); 
			if(move_uploaded_file($_FILES['file']['tmp_name'], $target_path)) {
				$response="OK";
			} else{
				$response="ERROR";
			}
		return 	new Response($response);			

    }

}
